<?php
// +----------------------------------------------------------------------
// | Name 风音框架
// +----------------------------------------------------------------------
// | Author 唐启云 <tqy@fxri.net>
// +----------------------------------------------------------------------
// | Copyright Copyright © 2016-2099 方弦研究所. All rights reserved.
// +----------------------------------------------------------------------
// | Link https://www.fxri.net
// +----------------------------------------------------------------------
namespace fxapp\facade_ext;

/**
 * 安全类
 */
class Safe
{
    /**
     * 解析数据-RSA私钥
     * @param mixed $var 变量
     * @param string $type 类型
     * @param array $param 参数
     * @return mixed
     */
    public function rsapriExt($var, $type, $param = [])
    {
        // 初始化变量
        $config = \fxapp\Base::config('safe.rsa');
        if (!$config['crypt_switch']) return $var;
        if (!is_string($var) || !is_array($param)) return false;
        $param = \fxapp\Param::merge($config['crypt_param'], $param);
        $predefined = [
            'secret' => $config['crypt_private'],
        ];
        $param = \fxapp\Param::define([$param, $predefined], '1.1.2');
        $data = \fxapp\Safe::rsapri($var, $type, $param);
        return $data;
    }

    /**
     * 解析数据-RSA公钥
     * @param mixed $var 变量
     * @param string $type 类型
     * @param array $param 参数
     * @return mixed
     */
    public function rsapubExt($var, $type, $param = [])
    {
        // 初始化变量
        $config = \fxapp\Base::config('safe.rsa');
        if (!$config['crypt_switch']) return $var;
        if (!is_string($var) || !is_array($param)) return false;
        $param = \fxapp\Param::merge($config['crypt_param'], $param);
        $predefined = [
            'secret' => $config['crypt_public'],
        ];
        $param = \fxapp\Param::define([$param, $predefined], '1.1.2');
        $data = \fxapp\Safe::rsapub($var, $type, $param);
        return $data;
    }
}
